#!/bin/bash


# generates plotting script for output
#
# we assume the isotropic phis get dumped to phi.out
#               isotropic Gs get dumpted to G.out

./plotspec

gnuscript="plot.plt"
gnubin=$(dirname $(which gnuplot))/gnuplot
if [ "$gnubin" == "" ]; then
  echo Error: cannot find gnuplot.
  exit 1;
fi;
#numks=`awk '{ x=NF; x=(x-1)/3.00; print x; exit; }' phi.out`
klist=`awk '{ c=2; while(c <= NF){ print $c" "; c+=1; }; exit; }' G0.out`

echo \#\!$gnubin > $gnuscript
echo >> $gnuscript
echo >> $gnuscript
echo \# Automatically generated by \'$0\' >> $gnuscript
echo >> $gnuscript
echo >> $gnuscript
echo set terminal postscript enhanced color lw 4 >> $gnuscript
echo >> $gnuscript
echo >> $gnuscript
##########################################################
# plot energy 
##########################################################
echo set output \"energy.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"Energy\" >> $gnuscript
echo set xlabel \"non-dimensional time\" >> $gnuscript
echo set ylabel \"E\" >> $gnuscript
echo >> $gnuscript
echo plot \"energy.out\" using 1:3 title \"\" w lines >> $gnuscript;
##########################################################
# plot energy 
##########################################################
echo set output \"denergy.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"Energy Change\" >> $gnuscript
echo set ylabel \"dE\" >> $gnuscript
echo >> $gnuscript
echo plot \"energy.out\" using 1:4 title \"\" w lines >> $gnuscript;

##############################################################
# now plot Gs
##############################################################
echo set output \"isotropic_G.ps\" >> $gnuscript;
echo >> $gnuscript
echo set yrange \[ \* \: 1.5 \] >> $gnuscript
echo set key top right >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) G\" >> $gnuscript
echo set ylabel \"G^0\" >> $gnuscript
echo G=\"G0.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=2;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"G using 1:$col title \"G^0 \($kmode\)\" w lines";
  col=$(($col+1));
done;
echo plot $plotlist >> $gnuscript

##############################################################
# now plot Phis
##############################################################
echo set output \"isotropic_unetPhi.ps\" >> $gnuscript;
echo >> $gnuscript
echo set yrange \[ \* \: *\] >> $gnuscript
echo set key top right >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) uneqtime Phi\(t,0\)\" >> $gnuscript
echo set ylabel \"Phi^0\" >> $gnuscript
echo f=\"Phi0uneqt.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=2;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"f using 1:$col title \"Phi^0 \($kmode\)\" w lines";
  col=$(($col+1));
done;
echo plot $plotlist >> $gnuscript

##############################################################
# now plot Phi spectra evolution 
##############################################################
echo set output \"energy_spectra.ps\" >> $gnuscript;
echo >> $gnuscript
echo set yrange \[ \* : \* \] >> $gnuscript
echo set key top right >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) sigma\(k\) Phi dk\" >> $gnuscript
echo set ylabel \"E\(k\)\" >> $gnuscript
echo f=\"spectra_0_real.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=2;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"f using 1:$col title \"E($kmode\)\" w lines";
  col=$(($col+1));
done;
echo plot $plotlist >> $gnuscript

##############################################################
# now plot dPhi spectra evolution 
##############################################################
echo set output \"Fenergy_spectra.ps\" >> $gnuscript;
echo >> $gnuscript
echo set key bottom right >> $gnuscript
echo set yrange \[ \* : \* \] >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) sigma\(k\) DPhiDt dk\" >> $gnuscript
echo set ylabel \"dE\(k\)/dt\" >> $gnuscript
echo f=\"Fspectra_0_real.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=2;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"f using 1:$col title \"dEdt($kmode\)\" w lines";
  col=$(($col+1));
done;
echo plot $plotlist >> $gnuscript



chmod u+x plot.plt
./plot.plt
exit;


##########################################################
# plot phis
##########################################################
echo set output \"isotropic_phi.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) Phi\" >> $gnuscript
echo set xlabel \"time step\" >> $gnuscript
echo set ylabel \"Phi^0\" >> $gnuscript
echo phi=\"phi.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=3;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"phi using 1:$col title \"Phi^0 \($kmode\)\" w lines";
  col=$(($col+2));
done;
echo plot $plotlist >> $gnuscript;

##############################################################
# now plot Gs
##############################################################
echo set output \"isotropic_G.ps\" >> $gnuscript;
echo >> $gnuscript
echo set key top right >> $gnuscript
echo set title \"Isotropic \(0^{th} mode\) G\" >> $gnuscript
echo set xlabel \"time step\" >> $gnuscript
echo set ylabel \"G^0\" >> $gnuscript
echo G=\"G.out\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=3;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"G using 1:$col title \"G^0 \(k=$kmode\)\" w lines";
  col=$(($col+2));
done;
echo plot $plotlist >> $gnuscript

##########################################################
# plot weighted phis
##########################################################
echo set output \"isotropic_weighted_phi.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"k^3 x Isotropic \(0^{th} mode\) Phi \(proportional to barotropic energy\)\" >> $gnuscript
echo set xlabel \"time step\" >> $gnuscript
echo set ylabel \"k^3 x Phi^0\" >> $gnuscript
echo >> $gnuscript
plotlist=
col=3;
for kmode in $klist; do
  if [ "$plotlist" != "" ]; then
	plotlist=$plotlist", ";
  fi
  plotlist=$plotlist"phi using 1:(\$$col*(\$$(($col-1))**3))  title \"Phi^0 \(k=$kmode\)\" w lines";
  col=$(($col+2));
done;
echo plot $plotlist >> $gnuscript;

##########################################################
# plot energy 
##########################################################
echo set output \"energy.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"Energy\" >> $gnuscript
echo set xlabel \"time step\" >> $gnuscript
echo set ylabel \"E\" >> $gnuscript
echo >> $gnuscript
echo plot \"energy.out\" using 1:2 title \"\" w lines >> $gnuscript;

##########################################################
# plot energy 
##########################################################
echo set output \"denergy.ps\" >> $gnuscript
echo >> $gnuscript
echo set key top left >> $gnuscript
echo set title \"Energy Change\" >> $gnuscript
echo set xlabel \"time step\" >> $gnuscript
echo set ylabel \"dE\" >> $gnuscript
echo >> $gnuscript
echo plot \"energy.out\" using 1:3 title \"\" w lines >> $gnuscript;

chmod u+x plot.plt
./plot.plt
